Extending Design Patterns into RIA
by Macromedia, Inc.
www.macromedia.com
Many developers use design patterns as a way to solve recurring problems. With the emergence of new technologies, such as Rich Internet Applications, you have the opportunity to architect more responsive applications that better reflect users needs. In doing so, some of the patterns you use to describe traditional server-based applications change to accommodate a more powerful client. The resulting change is evolutionary, however, and much of what you use today still applies in this new model. This article explores the differences between traditional server-based and Rich Internet Application models and describes high-level design patterns common to them. Later articles will look more closely at specific design patterns and how to apply them to create more powerful rich client applications.
Why Rich?
Rich Internet Applications are an evolution of the web development and deployment model. The world wide web and the HTML that built it was originally created to display information and to store random associations to that information. The web deployment model was so useful, however, that organizations soon began to use HTML to create interfaces to applications. In doing so, most applications took a significant step back in usability because of the delivery paradigm, the limited set of user interface controls, and the lack of a client-side data model.
In the early days of web application development, developers spent much of their time solving problems around data transport, resource allocation, business logic, and other infrastructure issues. As technologies to solve these problems matured and standardized, they made a web deployment model for applications much more attractive. While the back end matured however, the presentation layer did not. The advent of Rich Internet Applications allows developers to offer their users the functionality necessary for application interfaces, creating more usable applications and affecting the bottom line by making users more productive, reducing drop out rates and increasing the number of completed transactions.
The concept of richness has two aspects to it, richness in data model and richness in user interface. Rich in data means the user interface can represent and manipulate a more sophisticated data model residing inside of the client, one which can handle client-side computation and sending and receiving of data asynchronously. The advantage over pages created on the server and delivered to HTML is that the application resides on the client and provides a better user interaction with less server interaction. Being able to leverage a sophisticated data model on the client allows you to create highly responsive, interactive applications.
Rich also describes a vastly improved interface. HTML offers a restrictive set of user interface controls, whereas an RIA interface allows for inventive interface controls that can intelligently interact with the data model. The traditional Internet model uses a linear design, offering the user a set of choices that they then submit to the server in a batch mode. The user experience is dictated by the limitations of the technology that created it, instead of the goals of the application and the needs of the user. The constant process of server requests and page refreshes has several drawbacks including page latency, deprecated network bandwidth, and increased overhead to implement session or state across connections. With rich user interfaces, you can move from earlier models where server response affects the entire interface, to a model where changes are specific to the area of the application where the request originated. This, in essence, means that interfaces are broken down into individual component areas that are capable of localized change, server interaction, and inter client-side component communication.
The result is that you can now create client interfaces that much better reflect the richness and complexity of the data and logic coming from your application back end.
Rich Internet Application Design
Enterprise applications today are typically designed in a tiered fashion, where you organize application logic based on its purpose. The following diagram is typical of most n-tier applications:
Figure 1. A typical n-tier application
There are three major tiers: the client, the middle, and the enterprise information system (EIS) tier. Each major tier contains logic specific to the tier’s purpose. For example:
- The client tier contains the user interface and containers for the application interface
- The presentation tier contains logic to render content and handle user sessions and state management
- The business tier contains the business logic for the system
- The integration tier contains the connectors and adaptors that access remote processes and data sources
- The resource tier contains data from databases, enterprise information sources like ERP systems, and XML files
In a typical web deployed system, like in J2EE applications, the structure is as follows:
Figure 2. A typical web deployed system
In web deployed systems, the logic that determines application flow, content creation, and content delivery resides on the middle tier because the primary characteristic of the traditional web application is a request/response model with the browser as the client. To deliver an application interface, the system needs to completely format and render content in the form of HTML before it moves it off the middle tier and delivers it to the client. Modifying any part of the presentation requires a request to the server; the response includes an entire page, not just the modified portion. While applets and JavaScript/DHTML solutions offer an alternative to client tiers in a traditional web model, they have never become mainstream due to a variety of technical reasons, primarily around browser inconsistencies.
Moving to a rich client model allows you to reorganize the way you render a view to better reflect the structure of the application. See the following diagram:
Figure 3. A rich client model
Unlike J2EE applications, where the client request causes the system to construct a page and send it to the client, a Rich Internet Application leverages a much smaller unit, a component. Components range in size from the smallest poll question to a complete view or page. The RIA model breaks the interface into multiple touch points with which a user interacts and allows each point to communicate to the server directly.
This shift in application design from a page to a component focus allows the client tier to evolve and become smarter. Smarter clients no longer represent an end point to a server request but augment application capabilities, such as allowing application interaction without a server connection such as you would find in the requirements for occasionally connected applications.
The last characteristic to note in the emerging rich Internet model is the event model. Unlike the traditional model where the server receives the request and then creates the interface in top-down fashion, you can’t necessarily predict the sequence of events. Since each component is independent, it is not tied to a request that sends one response for the whole view. The ability for each component to send messages to the server requires that each component know how to handle responses from the server. In RIAs, asynchronous messaging enables you to control how the component creates messages to the server and the messages that respond to calls. With these workflow-driven application flows, you can decouple and separate application functionality for more granular control and leverage emerging service-oriented architectures.
Patterns in a Rich Internet Application Architecture
In the course of writing traditional web deployed applications, design patterns in enterprise development emerged to solve recurring problems. As much as the tier in a Rich Internet Application is an evolution from the tier in a traditional web model, so is the progression of the patterns used to build RIAs. Here, the term pattern refers to a generalized solution to a recurring design problem in software development.
The next section describes how you move functionality from the traditional front controller and business delegate patterns to new client patterns. As in any application design, you apply patterns when they suit your needs. The high-level patterns presented here address issues common to client/server communications; later articles will discuss these client patterns in more detail.
Two common problems in software development are how you communicate to your business logic and how you communicate from the client to the server. Establishing communication to business logic requires that you do so without coupling it so tightly to the view that a change (either in the logic or view) affects the entire application. When you communicate to the server, you need to ensure each request contains the necessary markup to establish security, session, or state management. You must establish each message to the server as a valid request or inspect the message for tokens used by the entire application.
There are a variety of languages that implement a traditional Internet model, the J2EE environment being prominent. In a Java environment, you often use a business delegate pattern to structure communication to business logic. This pattern, in its simplest form, uses a wrapper around the actual business logic. This wrapper translates an invocation request from the client to the proper request on the business logic component. To solve the server communication issue, many use the front controller pattern. This pattern routes all requests to a single controller file that validates security and establishes the correct flow of control so that it can create the correct response in the form of an HTML page. These two patterns often work together in a variety of composite structures.
Implementing a Rich Internet Application architecture is not terribly different from implementing a traditional web client, except that you move some of these activities from the server to the client. From an application design perspective, two high-level patterns emerge: service brokering and client component to server communication. Service brokering allows individual components to request an application service while abstracting the details of the service’s location or how it processes requests. The service broker provides the connectors for the individual component communication. In the client component to server communication, you provide connectivity from the component to the server instead of connecting at the page level. For each component, you provide the appropriate application interface logic, including asynchronous event handling. You can, of course, still create a centralized communication structure in your client to communicate to the server, depending on your application needs.
Service Broker
A primary goal for an application is to loosely couple the business logic and the interface logic so that changes in the application interface do not influence business logic, and the business logic requires no knowledge of how it is called. As noted, in a traditional environment, you might address this with a business delegate pattern. In a rich client you need to consider both the component and service requirements. The component requirements are as follows:
- Provide a standard interface to the remote service (standard methods for triggering methods, setting callbacks, and so forth).
- Abstract the remote service so it can be local or remote.
- Abstract how the call is made (SOAP, RMI, and so forth.
- Obfuscate when the call is made securely or anonymously.
- Provide handles to the components for incoming requests.
- Provide a location to connect to a service.
The service requirements include:
- Abstract the caller.
- Provide a standard way to trigger callback events.
One design that meets these requirements contains two types of classes, as shown Figure 4:
Figure 4. Design pattern for the service broker.
The first class, service broker, is responsible for creating the correct bridge adaptor to the remote service and maintaining a list of available services and their definitions.
The second class is a bridge component, a conduit between the user interface component and the service. The bridge component initializes based on the service definition and knows how to communicate to a local or remote service, knows how to communicate securely over different communication protocols, and knows which service to reference. The bridge contains the handle to the component so that it can provide direct callback methods to the component on success, failure, or request for more information from the service. The following is a diagram that shows this relationship:
Figure 5. The relationship between the classes
To understand this further, take a simple banking application for example. It includes variety of user options for users to manage their account and make inquires to the bank. The application is constructed in the tiered model noted in the previous section, where the client tier provides a rich interface that authenticates users, enables them to look at account balances and transactions, transfer money between accounts, or log a customer service request. The next section describes how you might construct the interface and focuses on how you set up communication from the client to the middleware tier.
The middleware tier contains the remote services. In the banking example, you might have a customerRequest service that resides on a different server since it ties into an existing Customer Relationship Management (CRM) system. The goal of a communication pattern is to keep the client interfaces from needing to know the details surrounding these services. In fact, all you need to know is the service's token name so you can request a connection to it through the service broker.
The service broker loads all of the services that the application knows of from a XML file that describe how to connect to the services, as well as the token name that you will use to reference the service. When the interface requests the customer service request, it passes the customer request token to the service broker. Upon receiving the request, the service broker finds the definition for the token and generates the necessary information for connecting, abstracting the details of how to process it from the rest of the interface.
A client structured in this way offers a lot of flexibility. For example, you can enhance the service broker so that it can detect disconnected status for the server and route requests to local services without the user interface component having to know the application state, allowing you to create occasionally connected applications.
Client Component to Server Communication
The design for client component to server communications must take into account the smaller and more granular components that comprise the interface, as well as the ability for components to contain other components and retain their independence. The requirements of the UI components include:
- Central control of component functionality
- Central control for message reception
- Central control for service communication
- Separate code to control the interface logic and the event management
This design uses four classes over which the user interface sits. The following diagram demonstrates:
Figure 6. Client component to server communication requires four class types.
The responsibilities for each class are as follows:
- The user interface is where the controls (calendar, data grids, check boxes, colors, etc.) reside.
- The view logic contains the code for manipulating the user interface, mapping data to fields, drawing data out and so on.
- The local data model represents the components that hold the data for the application.
-
The controller and mediator are often implemented as one component.
- The controller is responsible for controlling all processes for the user interface. It determines how to handle events from the remote server or from the user interface.
- The mediator is responsible for communicating externally from the component to the remote service. The mediator is also responsible for containing the methods that can be called back to from the remote service.
By designing a user interface in this way, the UI components remain independent, regardless of how you place them in the interface. By providing a common abstract interface for individual components, the mediator allows a UI component to behave independently or as part of a larger interface component. The following diagram describes the client architecture:
Figure 7. The client architecture
Take the bank application as an example. It includes an interface component that submits a customer service request to the bank. The service broker requests a handle to the remote service from the interface component so that it can invoke the appropriate methods. The interface consists of a variety of controls and a button that triggers an event. The view logic for the interface controller can capture this event. When the event is called, it triggers the controller and instructs the view logic to build a local data model for the transaction. Once the app populates the data model, the controller tells the mediator to submit a customer service request and pass the collected data. The mediator talks to the service broker to request the appropriate service and passes it a handle to itself. Once the mediator receives the information from the service broker it invokes a message that spawns the appropriate request. After the service finishes running on the server, it passes the responding message to the client. The bridge routes this responding message to the mediator, which determines which method to call on the controller and then calls that message so that it affects the interface appropriately.
There Is Always More
Of course any application architecture grows out of its unique requirements rather then being driven by a purist approach to design. This paper addresses two of the largest client patterns, as they are generally issues you need to address in any application development project. Later papers will address more specific patterns and the problems they solve.
Moving to Rich
Software development and the ideas that drive it continue to evolve as technology and the problem set evolves. The traditional model of web application development allowed organizations to make information and applications available to a very broad audience, but often at the cost of usability and user experience. Rich Internet Applications preserve the web deployment model but allow you to provide an intuitive, responsive user experience. In doing so, the ideas around how you structure your application evolve to take into account the new functionality and new use patterns. The result is that with slight shift in perspective, you can use all of your existing back-end services and architecture and yet offer your users a new, more powerful user experience.